/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/subsets-ii
   @Language: C++
   @Datetime: 19-07-08 16:21
   */

class Solution {
public:
	vector<vector<int>> subsetsWithDup(vector<int>& nums) {
		vector<int> A(nums);
		sort(A.begin(),A.end());
		vector<vector<int> > vs(1);
		for (int i=0,count=0; i<A.size(); ++i){
			int size = vs.size();
			for (int j=0; j<size; ++j){
				if (i==0 || A[i-1]!=A[i] || j>=count){
					vs.push_back(vs[j]);
					vs.back().push_back(A[i]);
				}
			}
			count=size;
		}
		return vs;
	}
};
